// pages/bookSearch/bookSearch.js.js
import { book } from "../../server/book";
import { file } from "../../server/file";
import { server } from "../../config/server";

let cache = {
    timeout: null
};

Page({

    /**
     * 页面的初始数据
     */
    data: {
        server_addr: server.server_addr,
        bookList: [],      // 所有书本
        bookName: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    /*
     * 返回上一页
     */
    returnMainPage() {
        wx.navigateBack({ delta: 1 });
    },

    // 输入任务名称的函数
    inputBookNameFunc(e) {
        this.setData({
            bookName: e.detail.value
        });
        if (!e.detail.value) {
            let timeout = cache.timeout;
            if (timeout) {
                clearTimeout(timeout);
            }
            cache.timeout = null;
        }
        this.debounceSearchBook(e.detail.value);
    },

    // 防抖函数搜索书籍
    debounceSearchBook(bookName) {
        let timeout = cache.timeout;
        if (timeout) {
            clearTimeout(timeout);
        }
        cache.timeout = setTimeout(() => {
            book.searchBook(bookName).then(async (res) => {
                let bookList = res.data;
                for (let i = 0; i < bookList.length; i++) {
                    let imgUrl = await file.downloadPublicFile(bookList[i].imgId);
                    console.log(imgUrl);
                    bookList[i].imgUrl = imgUrl.data.url;
                }
                this.setData({
                    bookList
                });
                console.log(res);
            }).catch((err) => {
                wx.showToast({
                    title: '请检查网络连接',
                    icon: 'none',
                    duration: 1000
                });
            });
        }, 1000);
    },

    // 跳转到参考书内容页面
    jmpBookContent(e) {
        let item= e.currentTarget.dataset.item;
        console.log('item', item);
        wx.navigateTo({
            url: '/pages/bookContent/bookContent?book='+item.id,
        });
    },

    /*
     * 加入书架
     */
    joinInLib(e) {
        // e.stopPropagation()
        let bookId = e.currentTarget.dataset.bookid;
        // 先找到书本对应的对象
        let bookList = this.data.bookList;
        for (let idx = 0; idx < bookList.length; idx++) {
            if (bookList[idx].id == bookId) {
                // 判断调用加入还是取消加入的函数
                if (bookList[idx].isJoin) {
                    // 取消加入
                    book.noJoinInLib(bookId).then((res) => {
                        wx.showToast({
                            title: '取消加入书架成功',
                            icon: 'none',
                            duration: 1000
                        });
                        bookList[idx].isJoin = false;
                        this.setData({
                            bookList
                        });
                    }).catch((err) => {
                        wx.showToast({
                            title: '请检查网络连接',
                            icon: 'none',
                            duration: 1000
                        });
                    });
                } else {
                    // 加入书架
                    book.joinInLib(bookId).then((res) => {
                        wx.showToast({
                            title: '加入书架成功',
                            icon: 'none',
                            duration: 1000
                        });
                        bookList[idx].isJoin = true;
                        this.setData({
                            bookList
                        });
                    }).catch((err) => {
                        wx.showToast({
                            title: '请检查网络连接',
                            icon: 'none',
                            duration: 1000
                        });
                    });
                }
                break;
            }
        }
    },
});